package com.pangsir.bookstore.service.impl;

import com.pangsir.bookstore.dao.OrderDao;
import com.pangsir.bookstore.pojo.CartItem;
import com.pangsir.bookstore.pojo.Order;
import com.pangsir.bookstore.pojo.OrderItem;
import com.pangsir.bookstore.pojo.User;
import com.pangsir.bookstore.service.CartItemService;
import com.pangsir.bookstore.service.OrderItemService;
import com.pangsir.bookstore.service.OrderService;

import java.util.Map;

public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao;
    private OrderItemService orderItemService;
    private CartItemService cartItemService;

    @Override
    public void addOrder(Order order) {
        orderDao.addOrder(order);
        User currUser = order.getOrderUser();
        Map<Integer, CartItem> cartItemMap = currUser.getCart().getCartItemMap();
        for (CartItem cartItem : cartItemMap.values()) {
            OrderItem orderItem = new OrderItem();
            orderItem.setBook(cartItem.getBook());
            orderItem.setBuyCount(cartItem.getBuyCount());
            orderItem.setOrderBean(order);
            orderItemService.addOrderItem(orderItem);
        }
        for (CartItem cartItem : cartItemMap.values()) {
            cartItemService.deleteCartItem(cartItem);
        }

    }
}
